<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        let str = "abcdefg"
        let k = 2
        function solution(str, k) {
            let strA = str.split('')
            let len = strA.length
            for(let i=0; i<len; i += 2*k) {
                // const arr = str.slice(i, k).split('').reverse().join('')
                // console.log(arr)
                let l = i - 1
                let r = i + k > len ? len : i + k
                while(++l < --r) {
                    [strA[i],strA[r]] = [strA[r], strA[i]]
                } 
            }
            return strA.join('')
        }
        const res = solution(str, k)
        console.log(res)
    </script>
</body>
</html>